﻿/* ============================================================================
** Author	: 1130282072@qq.com
** Comments	: 世界时间，心跳包，重连
** ----------------------------------------------------------------------------
** History	:	DATE			DESC
**				2017-7-23		Create by sunfei
** ============================================================================
*/

using com.guojin.core.io.message;
using com.guojin.mj.net.message.login;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class WorldTime : MonoBehaviour
{
    private static WorldTime instance = null;
    public static WorldTime Instance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<WorldTime>();
            }
            return instance;
        }
    }

    public  bool isSend714 = false;    //是否正在心跳倒计时
    public bool isConnecting = false;      //是否正在连接
    public void StartSendPingCountDown()
    {
        isSend714 = false;
        isConnecting = false;
    }

    public void ReSetHeartBeat()
    {
        StopCoroutine("CountDownTwoSecond");
        timer = 3f;
        isSend714 = false;
        isConnecting = true;
    }

    public void BreakLine()
    {
        SocketMgr.GetInstance().CloseSocket();
        isConnecting = true;

        UIManager.ChangeUI(UIManager.PageState.NetConnectPage, (GameObject obj) =>
        {
            obj.GetComponent<NetConnectPage>().InformationSetting();
        });
    }

    private float timer = 0f;
    private void Update()
    {
        if (!SceneManager.GetActiveScene().name.Equals("SHLogin"))
        {
            timer += Time.deltaTime;
            if (timer >= 8 && isSend714 == false && isConnecting == false)
            {
                timer = 0f;
                isSend714 = true;
                SendMsg714();
            }
        }
    }              

    private void SendMsg714()
    {
        StartCoroutine("CountDownTwoSecond");
        if (!SocketMessageQueue.open)
        {
            MessageManager.GetInstance().SendMsg714("1");
        }
    }    

    public IEnumerator CountDownTwoSecond()
    {
        yield return new WaitForSeconds(6f);
        if (isSend714 == true)
        {
            Debug.Log("心跳包服务器超2秒未响应");
            GameObject obj_0 = GameObject.Find("NetConnectPage(Clone)");
            if (obj_0)
            {
                Debug.Log("已经出现过断线面板了");
            }
            else
            {
                SocketMgr.GetInstance().CloseSocket();
                isConnecting = true;
                //SocketMgr.GetInstance().SetIsNetOpen(false);
                UIManager.ChangeUI(UIManager.PageState.NetConnectPage, (GameObject obj) =>
                {
                    obj.GetComponent<NetConnectPage>().InformationSetting();
                });
            }
        }
    }

    public void ReceiveMsg715(string msg, Message data)
    {
        SHPong pong = (SHPong)data;
        //停止2S不返还消息显示断线面板计时协程，将检查心跳方法设为false,开始新的3S发送ping计时
        Stoptimeout();
        SetIsSend714False(pong.time);
    }

    public void Stoptimeout()      //心跳包倒计时停止
    {
        StopCoroutine("CountDownTwoSecond");
    }

    public void SetIsSend714False(string serverTime)  
    {
        isSend714 = false;
    }
}